package com.wiloon.java8x.captcha;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: wiloon
 * Date: 9/30/12
 * Time: 2:15 PM
 */
public class CaptchaTest {
    static String filePath = "/home/wiloon/tmp/ems/rand.jpg";
    static String outPath = "/home/wiloon/tmp/ems/out.jpg";
    static String subImg = "/home/wiloon/tmp/ems/sub.jpg";

    public static void main(String[] args) {
        removeBackgroundTest();

    }

    public static void testSubWidth() {
        //read img in

        //cut img
        BufferedImage img = Captcha.getSubImg(filePath);
        writeImg(img, subImg);
        //calculate sub width
        int width = img.getWidth();
        int height = img.getHeight();
        double subWidth = (double) width / 6.0;
        System.out.println("width=" + width + ",subWidth=" + subWidth);
        System.out.println("height=" + height);
        //draw border
        for (int i = 0; i < 5; i++) {
            //int x = Math.round((i + 1) * subWidth);
            double dx = (i + 1) * subWidth;
            int x = (int) Math.round(dx);
            for (int y = 0; y < height; y++) {
                System.out.println(i + "," + x + "," + y);
                img.setRGB(x, y, Color.BLACK.getRGB());

            }
        }
        writeImg(img, outPath);
    }

    public static void subImg() {
        Captcha captcha = new Captcha();
        BufferedImage bi = captcha.getSubImg(filePath);
        writeImg(bi, outPath);
    }

    public static void removeBackgroundTest() {
        try {
            BufferedImage bi = Captcha.removeBackground(filePath);
            writeImg(bi, outPath);
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    private static void writeImg(BufferedImage bi, String path) {
        try {
            ImageIO.write(bi, "JPG", new File(path));
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
